package com.cf.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.cf.subject.application.convert.SubjectCategoryDtoConverter;
import com.cf.subject.application.dto.SubjectCategoryDto;
import com.cf.subject.common.entity.Result;
import com.cf.subject.domain.convert.SubjectCategoryConverter;
import com.cf.subject.domain.entity.SubjectCategoryBo;
import com.cf.subject.domain.service.SubjectCategoryDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 乘风破浪
 * @Date: 2024/3/2 15:25
 * @Description: 刷题分类controller
 **/
@RestController
@RequestMapping("subject/category")
@Slf4j
public class SubjectController {
    @Resource
    private SubjectCategoryDomainService categoryDomainService;


    @PostMapping("/add")
    public Result add(@RequestBody SubjectCategoryDto categoryDto) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.add.dto{}", categoryDto);
        }
        Preconditions.checkNotNull(categoryDto.getCategoryType(), "分类类型不能为空");
        Preconditions.checkArgument(StringUtils.isEmpty(categoryDto.getCategoryName()), "分类类型不能为空");
        Preconditions.checkNotNull(categoryDto.getParentId(), "父级id不能为空");
        SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.convertBotoCategory(categoryDto);
        categoryDomainService.add(subjectCategoryBo);
        return Result.ok();

    }

    @PostMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDto>> queryPrimaryCategory() {
        List<SubjectCategoryBo> categoryBoList = categoryDomainService.queryCategory(new SubjectCategoryBo());
        List<SubjectCategoryDto> subjectCategoryDtos = SubjectCategoryDtoConverter.INSTANCE.convertBotoCategory(categoryBoList);
        return Result.ok(subjectCategoryDtos);

    }

    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDto>> queryCategoryByPrimary(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.queryCategoryByPrimary.dto{}", JSON.toJSONString(subjectCategoryDto));
        }
        Preconditions.checkNotNull(subjectCategoryDto.getParentId(), "父级分类id不能为空");
        SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.subjectCategoryDtoConverter(subjectCategoryDto);
        List<SubjectCategoryBo> categoryBoList = categoryDomainService.queryCategory(subjectCategoryBo);
        List<SubjectCategoryDto> subjectCategoryDtos = SubjectCategoryDtoConverter.INSTANCE.convertBotoCategory(categoryBoList);
        return Result.ok(subjectCategoryDtos);
    }
    @PostMapping("/update")
    public Result<List<SubjectCategoryDto>> updateSubject(@RequestBody SubjectCategoryDto subjectCategoryDto) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.updateSubject.dto{}", JSON.toJSONString(subjectCategoryDto));
        }
        Preconditions.checkNotNull(subjectCategoryDto.getId(), "分类id不能为空");
        SubjectCategoryBo subjectCategoryBo = SubjectCategoryDtoConverter.INSTANCE.subjectCategoryDtoConverter(subjectCategoryDto);
        categoryDomainService.updateSubject(subjectCategoryBo);
        return Result.ok();
    }
    @DeleteMapping("/delete/{id}")
    public Result<List<SubjectCategoryDto>> deleteSubject(@PathVariable("id") Long id) {
        if (log.isInfoEnabled()) {
            log.info("SubjectController.deletedSubject.id{}", id);
        }
        Preconditions.checkNotNull(id, "分类id不能为空");
        categoryDomainService.deleteSubject(id);
        return Result.ok();
    }
}
